<?php
//验证token
$wx = new Wx();
 
class Wx {
    // 微信后台设置的token值 php7.1之后可以加权限 private
    const TOKEN = 'weixin';
 
    // 构造方法
    public function __construct(){
        // 判断是否是第1次接入 echostr
        if (!empty($_GET['echostr'])) {
            echo $this->checkSignature();
        }else{
            // 接受处理数据
            $this->acceptMsg();
        }
    }
 
    /**
     * 初次接入校验
     * @return [type] [description]
     */
    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
 
 
    
}